/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entity.MDoctor;
import entity.MHospital;
import entity.SrvCureservice;
import java.math.BigDecimal;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import session.SrvCureserviceFacade;

/**
 *
 * @author Munliga
 */
@Stateless
public class SrvCureserviceFacade extends AbstractFacade<SrvCureservice> {
    @PersistenceContext(unitName = "MedicalHub-warPU")
    private EntityManager em;
    @EJB
    private SrvCureserviceFacade srvCureserviceFacade;
    @EJB
    private MDoctorFacade mDoctorFacade;
    @EJB
    private MHospitalFacade mHospitalFacade;

    protected EntityManager getEntityManager() {
        return em;
    }

    public SrvCureserviceFacade() {
        super(SrvCureservice.class);
    }

    //Create Query
    public List<SrvCureservice> LoadDataService(String txtName, String txtPrice, String txtCurrency, String txtDoctorName, String txtHospitalKeyword, String sortIndex, String sortDirection) {
        try {
            String strQuery = "SELECT s FROM SrvCureservice s ";
            String strWhere = " Where 1=1 ";
            strWhere += (!txtName.isEmpty()) ? " AND s.name Like '%" + txtName + "%' " : " ";
            strWhere += (!txtPrice.isEmpty()) ? " AND s.price <= " + Integer.parseInt(txtPrice) + " " : " ";
            strWhere += (!txtCurrency.isEmpty()) ? " AND s.currency Like '%" + txtCurrency + "%' " : " ";
            strWhere += (!txtDoctorName.isEmpty()) ? " AND CONCAT(s.mDoctor.name,' ',s.mDoctor.surname) Like '%" + txtDoctorName + "%' " : " ";
            strWhere += (!txtHospitalKeyword.isEmpty()) ? " AND CONCAT(s.mHospital.name,' ',s.mHospital.location,' ',s.mHospital.hospitalLevel) Like '%" + txtHospitalKeyword + "%' " : " ";
            strWhere += "ORDER BY s." + sortIndex + " " + sortDirection;
            strQuery += strWhere;
            return em.createQuery(strQuery).getResultList();
        } catch (Exception e) {
            return null;
        }
    }

    public List<SrvCureservice> findByServiceId(String id) {
        return em.createNamedQuery("SrvCureservice.findById").setParameter("id", Long.parseLong(id)).getResultList();
    }

    public void EditService(String id, String txtName, String txtPrice, String txtCurrency, String ddlDoctorId, String ddlHospitalId){
        SrvCureservice editService = srvCureserviceFacade.find(Long.parseLong(id));
//        MDoctor mDoctor = mDoctorFacade.find(Long.parseLong(ddlDoctorId));
//        MHospital mHospital = mHospitalFacade.find(Long.parseLong(ddlHospitalId));
        editService.setName(txtName);
        editService.setPrice(new BigDecimal(txtPrice));
        editService.setCurrency(txtCurrency);
        if (!ddlDoctorId.isEmpty()){
            MDoctor mDoctor = mDoctorFacade.find(Long.parseLong(ddlDoctorId));
            editService.setMDoctor(mDoctor);
        }else{
            editService.setMDoctor(null);
        }
        if (!ddlHospitalId.isEmpty()){
            MHospital mHospital = mHospitalFacade.find(Long.parseLong(ddlHospitalId));
            editService.setMHospital(mHospital);
        }else{
            editService.setMHospital(null);
        }
        em.persist(editService);
    }

    public void DeleteService(String id){
        SrvCureservice deleteService = srvCureserviceFacade.find(Long.parseLong(id));
        em.remove(deleteService);
    }

    public void AddService(String txtName, String txtPrice, String txtCurrency, String ddlDoctorId, String ddlHospitalId){
//        MDoctor mDoctor = mDoctorFacade.find(Long.parseLong(ddlDoctorId));
//        MHospital mHospital = mHospitalFacade.find(Long.parseLong(ddlHospitalId));
        SrvCureservice addService = new SrvCureservice();
        addService.setName(txtName);
        addService.setPrice(new BigDecimal(txtPrice));
        addService.setCurrency(txtCurrency);
        if (!ddlDoctorId.isEmpty()){
            MDoctor mDoctor = mDoctorFacade.find(Long.parseLong(ddlDoctorId));
            addService.setMDoctor(mDoctor);
        }else{
            addService.setMDoctor(null);
        }
        if (!ddlHospitalId.isEmpty()){
            MHospital mHospital = mHospitalFacade.find(Long.parseLong(ddlHospitalId));
            addService.setMHospital(mHospital);
        }else{
            addService.setMHospital(null);
        }
        em.persist(addService);
    }

}
